package club.ximeng.flutter_restart_app;

import android.annotation.SuppressLint;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

import androidx.annotation.NonNull;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;

/**
 * FlutterRestartAppPlugin
 */
public class FlutterRestartAppPlugin implements FlutterPlugin, MethodCallHandler {
  /// The MethodChannel that will the communication between Flutter and native Android
  ///
  /// This local reference serves to register the plugin with the Flutter Engine and unregister it
  /// when the Flutter Engine is detached from the Activity
  private MethodChannel channel;
  private Context context;

  @Override
  public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
    context = flutterPluginBinding.getApplicationContext();
    channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "flutter_restart_app");
    channel.setMethodCallHandler(this);
  }

  @Override
  public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
    switch (call.method) {
      case "getPlatformVersion":
        result.success("Android " + android.os.Build.VERSION.RELEASE);
        break;
      case "restart":
//        activity.recreate();
        restartApp12();
        result.success(true);
        break;
      default:
        result.notImplemented();
    }
  }

  private void restartApp12() {
    System.out.println("restartApp。。。。。。。。。。。。。");
    Intent intent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
    PendingIntent restartIntent;
    // 安卓版本 >= 12
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
      restartIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE);
    } else {
      restartIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);
    }
    AlarmManager mgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    // 100 毫秒后重启应用
    mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, restartIntent);
    // 退出程序
    android.os.Process.killProcess(android.os.Process.myPid());
  }

  private void restartApp2() {
    Intent intent = Intent.makeRestartActivityTask(
        context.getPackageManager()
            .getLaunchIntentForPackage(context.getPackageName())
            .getComponent());
    @SuppressLint("WrongConstant") PendingIntent restartIntent = PendingIntent
        .getActivity(context, 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
    //退出程序
    AlarmManager mgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    assert mgr != null;
    // 1秒钟后重启应用
    mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 1000, restartIntent);
    // 退出程序
    android.os.Process.killProcess(android.os.Process.myPid());
    System.exit(1);
  }

  @Override
  public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
    channel.setMethodCallHandler(null);
  }
}
